home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: Want function of strmp with wild characters
- Date: Wed, 31 Jan 1996 16:04:03 +0200
- Organization: Carelcomp Forest
- Message-ID: <310F76D3.2924@cmt.lpr.mail.carel.fi>
- References: <1996Jan30.213213.16764@schbbs.mot.com>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
-
- Ashok Patil wrote:
- >
- > HI:
- > I need a function that compares two strings with
- > wild cards. like one string is "Hello" and another is
- > is "Helo*". The comparison of these two should return
- > 0. And if it works for ? that would be great.
- >
- > If you have a function, please mail it to
- > Ashokman@aol.com
- >
- > Thanks a lot
- >
- > Ashok
-
- You're welcome.
- int WildCmp(const char *s1, const char *s2)
- {
- while (*s1) {
- switch (*s1) {
- case '?':
- break;
- case '*': {
- char tmp = *s2;
-
- s1++;
- while (*s2 && *s2 != *s1)
- s2++;
- if (*s2 != *s1)
- return (*s1 < tmp) ? -1 : 1;
- }
- if (!*s1)
- return 0;
- break;
- default:
- if (*s1 < *s2)
- return -1;
- if (*s1 > *s2)
- return 1;
- }
- if (!*s1 && *s2)
- return -1;
- if (*s1 && !*s2)
- return 1;
- s1++;
- s2++;
- }
- return (!*s1 && *s2) ? -1 : (*s1 && !*s2) ? 1 : 0;
- }
-
- #ifdef TEST
- #include <stdio.h>
- #include <assert.h>
- int main(int argc, char **argv)
- {
- assert(argc == 3);
- printf("Str match: %d\n", WildCmp(argv[1], argv[2]));
- return 0;
- }
- #endif
-
- Later,
- AriL
- --
- All my opinions are mine and mine alone.
-